using SmartCommunity.Services.Workorder;
using Microsoft.AspNetCore.Mvc;

namespace SmartCommunity.Controllers.Workorder
{
    /// <summary>
    /// API接口：工单池
    /// </summary>
    [ApiController]
    [Route("api/workorder/[controller]")]
    public class WorkOrderController : BaseController
    {
        #region 构造函数
        private readonly WorkOrderService workOrderService;
        public WorkOrderController(WorkOrderService workOrderService)
        {
            this.workOrderService = workOrderService;
        }
        #endregion

        #region 查询导出 
        /// <summary>
        /// 分页查询
        /// </summary>
        [HttpGet]
        public async Task<PageResult<WorkOrderDto>> GetList([FromQuery] WorkOrderQuery query, [FromQuery] Pager pager)
        {
            return await workOrderService.FindPagedList(query, pager, CurrentUser);
        }

        /// <summary>
        /// 通过主键查询
        /// </summary>
        /// <param name="id">主键ID</param>
        [HttpGet("{id}")]
        public async Task<WorkOrderDto> GetOne(Guid id)
        {
           return await workOrderService.FindWorkOrderById(id);
        }

        /// <summary>
        /// 导出excel
        /// </summary>
        [HttpGet("export")]
        public async Task<ActionResult> ExportExcel([FromQuery] WorkOrderQuery query)
        {
            var pageData = await workOrderService.FindPagedList(query, Pager.Unpaged, CurrentUser);
            var exporter = new ExcelExporter();
            exporter.AddTitleRow(0, "工单编号", "工单类型", "工单描述", "工单状态", "提交人", "提交人电话", "小区ID", "提交人房号", "提交人地址", "提交时间", "评价星级", "评价内容", "工单处理人账号", "工单处理人姓名", "处理结果");
            int rowIndex = 1;
            foreach (var data in pageData.DataList)
            {
                exporter.AddDataRow(rowIndex++, 0, data.OrderNo, data.OrderType, data.Description, data.OrderStatus, data.SubmitPerson, data.SubmitTel, data.SubmitAreaId, data.SubmitRoomId, data.SubmitAddr, data.SubmitTime, data.RateStars, data.RateContent, data.HandleUser, data.HandleUserName, data.HandleResult );
            }
            var bytes = exporter.GetExcelData();
            return File(bytes, CONTENT_TYPE_EXCEL, $"工单池导出{DateTime.Now:yyyyMMddHHmmss}.xlsx");
        }
        #endregion

        #region 增 删 改
        /// <summary>
        /// 新增工单池
        /// </summary>
        [HttpPost]
        [Log("新增工单池", OperateType.Create, false, true)]
        public async Task<ActionResult<WorkOrderDto>> Create([FromBody] WorkOrderDto dto)
        {
            var workOrder = await workOrderService.CreateWorkOrder(dto, CurrentUser);
            return CreatedAtAction("GetOne", new { id = workOrder.OrderId }, workOrder);
        }

        /// <summary>
        /// 修改工单池
        /// </summary>
        [HttpPut("{id}")]
        [Log("修改工单池", OperateType.Update)]
        public async Task<ActionResult> Update(Guid id, [FromBody] WorkOrderDto dto)
        {
            if (id != dto.OrderId)
            {
                return ErrorMessage("要更新的数据和ID不匹配");
            }
            await workOrderService.UpdateWorkOrder(dto, CurrentUser);
            return NoContent();
        }

        /// <summary>
        /// 删除工单池
        /// </summary>
        [HttpDelete]
        [Log("删除工单池", OperateType.Delete)]
        public async Task<ActionResult> Delete([FromQuery] Guid[] ids)
        {
            if (ids.Length == 0)
            {
                return ErrorMessage("参数错误：没有接收到要删除的主键值");
            }
            await workOrderService.DeleteByIds(ids, CurrentUser);
            return NoContent();
        }
        #endregion
    }
}
